/**
 * Copyright (c)  TMA Mobile Solutions, TMA Solutions company. All Rights Reserved.
 * This software is the confidential and proprietary information of TMA Solutions, 
 * ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only in accordance 
 * with the terms of the license agreement you entered into with TMA Solutions.
 *
 *
 * TMA SOLUTIONS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, 
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. TMA SOLUTIONS SHALL NOT BE LIABLE FOR ANY DAMAGES 
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
 */
package com.example.pregnant;

import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.TimeUtils;

/** 
 * Class description: TODO
 * 
 * @created           May 18, 2011
 * @version 		  v2.0
 * @author 			  nmhien
 * @copyright         TMA Solutions, www.tmasolutions.com
 */
public class Settings {
	public static Context mContext; 
	
	public static String IS_STARTED = "IS_STARTED";
	public static String START_DATE = "START_DATE";
//	public static String START_MONTH = "START_MONTH";
//	public static String START_YEAR = "START_YEAR";
	public static String WEEK_AGE = "WEEK_AGE";
//	public static long getRemainDays(){
//		if (getStartDay() != -1 && getStartMonth() != -1 && getStartYear() != -1){
//			Calendar startDate = Calendar.getInstance();
//			Calendar currentDate = Calendar.getInstance();
////			Date startDate = new Date(getStartYear(), getStartMonth(), getStartDay());
////			Date currentDate = Calendar.getInstance().getTime();
//			startDate.set(getStartYear(), getStartMonth(), getStartDay());
//			
//			long diff = currentDate.getTimeInMillis() - startDate.getTimeInMillis();
//			long diffDays = diff / (24 * 60 * 60 * 1000);
//			return diffDays;
//		} else 
//			return 0;
//	}
	
	public static void saveWeekAges(){
		int age = PreferenceManager.getDefaultSharedPreferences(mContext).getInt(WEEK_AGE, 1);
		Calendar startDate = Calendar.getInstance();
		startDate.add(Calendar.WEEK_OF_YEAR, -age);
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
		Editor editor = settings.edit();
		editor.putLong(START_DATE, startDate.getTimeInMillis());
		editor.commit();
	}
	
	public static int getWeekAge(){
		long startDate = PreferenceManager.getDefaultSharedPreferences(mContext).getLong(START_DATE, 0);
		
		Calendar currentDate = Calendar.getInstance();
		long diff = currentDate.getTimeInMillis() - startDate;
		long diffWeek = diff / (7* 24 * 60 * 60 * 1000);
		if (diffWeek >= 40) diffWeek = 40;
		if (diffWeek <= 1) diffWeek = 1;
		
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
		Editor editor = settings.edit();
		editor.putInt(WEEK_AGE, (int)diffWeek);
		editor.commit();
		
		Log.v("@nmh", "diff Week:" + diffWeek);
		return (int) diffWeek;
	}
	
//	public static int getStartDay(){
//		return PreferenceManager.getDefaultSharedPreferences(mContext).getInt(START_DAY, -1);
//	}
//	public static int getStartMonth(){
//		return PreferenceManager.getDefaultSharedPreferences(mContext).getInt(START_MONTH, -1);
//	}
//	public static int getStartYear(){
//		return PreferenceManager.getDefaultSharedPreferences(mContext).getInt(START_YEAR, -1);
//	}
}
